import { ref } from 'vue'
import { defineStore } from 'pinia'
import axios from 'axios'
import { userId } from './user'
import { friendId, type } from './friend'
import { reverseList } from './chatRecording'


export const useSendChatMessageStore = defineStore('sendChatMessage', () =>{
    const messageType = ref('text')
    const content = ref('')

    function send(){

        if(content.value === '' || content.value === null){
            return
        }
        
        const formData = new FormData()

        formData.append('sendUserId', userId.value)
        formData.append('receiveUserId', friendId.value)
        formData.append('messageType', messageType.value)
        formData.append('content', content.value)

        const url = ref('')
        if(type.value === 'group'){
            url.value = 'https://47.120.69.214:8787/chat/sendMessageToGroup'
        }
        else{
            url.value = 'https://47.120.69.214:8787/chat/sendMessageToUser'
        }

        axios.post(url.value, formData)
        .then(response => response.data)
        .catch(error => console.log(error))
        // if(userId.value !== friendId.value || type.value === 'group'){
        reverseList.value.push({
            "messageType" : messageType.value,
            "content" : content.value,
            "sendUserId" : userId.value,
            "receiveUserId" : friendId.value
        })
        // console.log(reverseList.value)
    // }
        content.value = ''
    }



    return {
        messageType,
        content,
        send
    }
})


